<?php

/**
 *    文章管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class ActivityApp extends BackendApp
{
	var $_activity_mod;
	var $_uploadedfile_mod;

	function __construct()
	{
		$this->ArticleApp();
	}

	function ArticleApp()
	{
		parent::BackendApp();

		$this->_activity_mod =& m('activity');
		$this->_uploadedfile_mod = &m('uploadedfile');
	}

	/**
	 *    文章索引
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
			
		$conditions='';
		$conditions .= $this->_get_query_conditions(array(
		array(
                'field' => 'title',         //可搜索字段title
                'equal' => 'LIKE',          //等价关系,可以是LIKE, =, <, >, <>
                'assoc' => 'AND',           //关系类型,可以是AND, OR
                'name'  => 'title',         //GET的值的访问键名
                'type'  => 'string',        //GET的值的类型
		),
		array(
                'field' => 'cat',                
                'assoc' => 'AND',
                'name' => 'cat',
                'type' => 'string',
		),
		));
		$page   =   $this->_get_page(10);   //获取分页信息
		$articles=$this->_activity_mod->find(array(
        'fields'   => ' * ',
        'conditions'  => ' 1=1 ' . $conditions,
        'limit'   => $page['limit'],

        'order'   => ' act_id DESC', //必须加别名
        'count'   => true   //允许统计
		));    //找出所有的文章

		$sql="select  * from   cw_dictionary where  d_code='cat'";//
	 $dis_list = $this->_activity_mod->GetAll($sql);
	 $dis_cat=array();
	 foreach ($dis_list as $k=>$v){
	 	$dis_cat[$v['d_key']]=$v['d_value'];
	 }
	 $this->assign('dis_cat', $dis_cat);
		$page['item_count']=$this->_activity_mod->getCount();   //获取统计数据

			
		$this->_format_page($page);
		$this->import_resource(array('script' => 'inline_edit.js'));
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件

		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('articles', $articles);
		$this->display('activity.index.html');
	}
	/**
	 *    新增文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function add()
	{
		if (!IS_POST)
		{
			/* 显示新增表单 */

			$activity = array('sort_order' => 255);
			$this->assign("id", 0);
			$this->assign('activity', $activity);

			$sql="select  * from   cw_dictionary where  d_code='cat'";//
			$dis_list = $this->_activity_mod->GetAll($sql);
			$dis_cat=array();
			foreach ($dis_list as $k=>$v){
				$dis_cat[$v['d_key']]=$v['d_value'];
			}
			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			$this->assign('dis_cat', $dis_cat);
			$this->display('activity.form.html');
		}
		else
		{
			$data = array();
			$data['title']      =   $_POST['title'];
			$data['cat']    =   $_POST['cat'];
			$data['subject']    =   $_POST['subject'];
			$data['sort_order'] =   $_POST['sort_order'];
			$data['content'] =   $_POST['article_content'];

			if (!$article_id = $this->_activity_mod->add($data))  //获取article_id
			{
				$this->show_warning($this->_activity_mod->get_error());

				return;
			}

			/* 处理上传的图片 */
			$logo       =   $this->_upload_logo($article_id);
			if ($logo === false)
			{
				return;
			}
			$logo && $this->_activity_mod->edit($article_id, array('pic' => $logo)); //将logo地址记下

			$this->show_message('add_activity_successed',
                'back_list',    'index.php?app=activity',
                'continue_add', 'index.php?app=activity&amp;act=add'
                );
		}
	}
	/**
	 *    编辑文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function edit()
	{
		$article_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if (!$article_id)
		{
			$this->show_warning('no_such_activity');
			return;
		}
		if (!IS_POST)
		{
			$activity     = $this->_activity_mod->find($article_id);

			if (empty($activity))
			{
				$this->show_warning('no_such_activity');
				return;
			}
			$sql="select  * from   cw_dictionary where  d_code='cat'";//
			$dis_list = $this->_activity_mod->GetAll($sql);
			$dis_cat=array();
			foreach ($dis_list as $k=>$v){
				$dis_cat[$v['d_key']]=$v['d_value'];
			}
			$this->assign('dis_cat', $dis_cat);
			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			$activity    =   current($activity);
			$this->assign('activity', $activity);
			$this->display('activity.form.html');
		}
		else
		{
			$data = array();
			$data['title']      =   $_POST['title'];
			$data['cat']    =   $_POST['cat'];
			$data['subject']    =   $_POST['subject'];
			$data['sort_order'] =   $_POST['sort_order'];
			$data['content'] =   $_POST['article_content'];


			$rows=$this->_activity_mod->edit($article_id, $data);
			if ($this->_activity_mod->has_error())
			{
				$this->show_warning($this->_activity_mod->get_error());

				return;
			}

			/* 处理上传的图片 */
			$logo       =   $this->_upload_logo($article_id);
			if ($logo === false)
			{
				return;
			}
			$logo && $this->_activity_mod->edit($article_id, array('pic' => $logo)); //将logo地址记下

			$this->show_message('edit_activity_successed',
                'back_list',        'index.php?app=activity',
                'edit_again',    'index.php?app=activity&amp;act=edit&amp;id=' . $article_id);
		}
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('if_show', 'sort_order')))
		{
			$data[$column] = $value;
			$this->_activity_mod->edit($id, $data);
			if(!$this->_activity_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}
	function drop()
	{
		$article_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$article_ids)
		{
			$this->show_warning('no_such_activity');

			return;
		}
		$article_ids=explode(',', $article_ids);
		$this->_activity_mod->drop($article_ids);
		if ($this->_activity_mod->has_error())    //删除
		{
			$this->show_warning($this->_activity_mod->get_error());

			return;
		}

		$this->show_message('drop_ok');
	}

	/* 更新排序 */
	function update_order()
	{
		if (empty($_GET['id']))
		{
			$this->show_warning('Hacking Attempt');
			return;
		}

		$ids = explode(',', $_GET['id']);
		$sort_orders = explode(',', $_GET['sort_order']);
		foreach ($ids as $key => $id)
		{
			$this->_article_mod->edit($id, array('sort_order' => $sort_orders[$key]));
		}

		$this->show_message('update_order_ok');
	}

	/* 构造并返回树 */
	function &_tree($acategories)
	{
		import('tree.lib');
		$tree = new Tree();
		$tree->setTree($acategories, 'cate_id', 'parent_id', 'cate_name');
		return $tree;
	}
	/* 取得可以作为上级的文章分类数据 */
	function _get_options()
	{
		$mod_acategory = &m('acategory');
		$acategorys = $mod_acategory->get_list();
		$tree =& $this->_tree($acategorys);
		return $tree->getOptions();
	}

	/* 异步删除附件 */
	function drop_uploadedfile()
	{
		$file_id = isset($_GET['file_id']) ? intval($_GET['file_id']) : 0;
		if ($file_id && $this->_uploadedfile_mod->drop($file_id))
		{
			$this->json_result('drop_ok');
			return;
		}
		else
		{
			$this->json_error('drop_error');
			return;
		}
	}



	function _upload_logo($brand_id)
	{
		$file = $_FILES['pic'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			return '';
		}
		import('uploader.lib');             //导入上传类
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
		$uploader->addFile($_FILES['pic']);//上传logo

		if (!$uploader->file_info())
		{
			$this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=activity&amp;act=edit&amp;id=' . $brand_id);
			return false;
		}
		// 指定保存位置的根目录
		$uploader->root_dir(ROOT_PATH);
		$imgname=gmtime();
		// 上传
		if ($file_path = $uploader->save('data/files/mall/activity', $imgname))   //保存到指定目录，并以指定文件名$brand_id存储
		{
			return $file_path;
		}
		else
		{
			return false;
		}
	}
}

?>